package tool

type MyInt interface {
	int | int8 | int32 // 作用域泛型的，而不是一个接口方法
}

type ListNode struct {
	Val  int
	Next *ListNode
}

type Node1[T MyInt] struct {
	Value T
	Next  *Node1[T]
}

type DoubleNode[T MyInt] struct {
	Value T
	Next  *DoubleNode[T]
	Pre   *DoubleNode[T]
}

func (n *DoubleNode[T]) DoubleNode(data T) {
	n.Value = data

}

type DoubleNodeDoubleQueue[T MyInt] struct {
	head *DoubleNode[T]
	tail *DoubleNode[T]
	size int
}

func (Q *DoubleNodeDoubleQueue[T]) IsEmpty() bool {

	return Q.size == 0
}

func (Q *DoubleNodeDoubleQueue[T]) Size() int {
	return Q.size

}
func (Q *DoubleNodeDoubleQueue[T]) PushHead(value T) {
	cur := DoubleNode[T]{Value: value}
	if Q.head == nil {
		Q.head = &cur
		Q.tail = &cur
	} else {
		cur.Next = Q.head
		Q.head.Pre = &cur
		Q.head = &cur
	}

	Q.size++

}
func (Q *DoubleNodeDoubleQueue[T]) PushTail(value T) {
	cur := DoubleNode[T]{Value: value}
	if Q.head == nil {
		Q.head = &cur
		Q.tail = &cur
	} else {
		Q.tail.Next = &cur
		cur.Pre = Q.tail
		Q.tail = &cur

	}

	Q.size++
}

func (Q *DoubleNodeDoubleQueue[T]) PopHead() T {
	var ans T = -1
	if Q.head == nil {
		Q.tail = nil

	} else if Q.Size() == 1 {
		Q.head = nil
		Q.tail = nil
		ans = Q.head.Value
	} else {
		next := Q.head.Next
		next.Pre = nil
		Q.head.Next = nil
		ans = Q.head.Value
		Q.head = next
	}

	if Q.Size() > 0 {
		Q.size--

	}
	return ans
}
func (Q *DoubleNodeDoubleQueue[T]) PopTail() T {
	var ans T = -1
	if Q.tail == nil {
		Q.head = nil

	} else if Q.Size() == 1 {
		ans = Q.tail.Value
		Q.head = nil
		Q.tail = nil
	} else {
		pre := Q.tail.Pre
		pre.Next = nil
		Q.tail.Pre = nil
		ans = Q.tail.Value
		Q.tail = pre
	}
	if Q.Size() > 0 {
		Q.size--

	}
	return ans
}
